package com.xcyy.clbzbh.controller;

import com.xcyy.clbzbh.VO.CityVO;
import com.xcyy.clbzbh.VO.ResultVO;
import com.xcyy.clbzbh.VO.TongJiVO;
import com.xcyy.clbzbh.common.ResultVOUtil;
import com.xcyy.clbzbh.entity.Benchmark;
import com.xcyy.clbzbh.entity.GPSPoint;
import com.xcyy.clbzbh.service.BenchmarkService;
import com.xcyy.clbzbh.service.GPSPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class CheckController {
    @Autowired
    private BenchmarkService benchmarkService;
    @Autowired
    private GPSPointService gpsPointService;

    @PostMapping("/check")
    private ResultVO check(@RequestParam(value = "shyj",required = false)String shyj
            , String type, String id, String username,String status){

        if ("gpspoint".equals(type)){
            GPSPoint gpsPoint=new GPSPoint();
            gpsPoint.setId(id);
            gpsPoint.setStatus(status);
            gpsPoint.setShr(username);
            gpsPoint.setShyj(shyj);
            gpsPoint.setShday(new Date());
            GPSPoint result=gpsPointService.save(gpsPoint);

            return ResultVOUtil.success(result);
        }else if ("benchmark".equals(type)){
            Benchmark benchmark=new Benchmark();
            benchmark.setId(id);
            benchmark.setStatus(status);
            benchmark.setShr(username);
            benchmark.setShyj(shyj);
            benchmark.setShday(new Date());
            Benchmark result=benchmarkService.save(benchmark);
            return ResultVOUtil.success(result);
        }

        return ResultVOUtil.error(401,"类型错误！");
    }

    @PostMapping("/total")
    public ResultVO total(){
        TongJiVO benchmarkVO=new TongJiVO();
        TongJiVO gpsVO=new TongJiVO();
        Object totalBenchmark=benchmarkService.totalbenchmark();
        benchmarkVO.setRkwcs( Integer.parseInt(( ((Object[]) totalBenchmark)[0]).toString()));
        benchmarkVO.setFfwcs( Integer.parseInt(( ((Object[]) totalBenchmark)[1]).toString()));
        benchmarkVO.setTjwcs( Integer.parseInt(( ((Object[]) totalBenchmark)[2]).toString()));
        benchmarkVO.setShwcs( Integer.parseInt(( ((Object[]) totalBenchmark)[3]).toString()));
        benchmarkVO.setBhwcs( Integer.parseInt(( ((Object[]) totalBenchmark)[4]).toString()));
        benchmarkVO.setZs( Integer.parseInt(( ((Object[]) totalBenchmark)[5]).toString()));
        Object totalGpsPoint=gpsPointService.totalgpspoint();
        gpsVO.setRkwcs( Integer.parseInt(( ((Object[]) totalGpsPoint)[0]).toString()));
        gpsVO.setFfwcs( Integer.parseInt(( ((Object[]) totalGpsPoint)[1]).toString()));
        gpsVO.setTjwcs( Integer.parseInt(( ((Object[]) totalGpsPoint)[2]).toString()));
        gpsVO.setShwcs( Integer.parseInt(( ((Object[]) totalGpsPoint)[3]).toString()));
        gpsVO.setBhwcs( Integer.parseInt(( ((Object[]) totalGpsPoint)[4]).toString()));
        gpsVO.setZs( Integer.parseInt(( ((Object[]) totalGpsPoint)[5]).toString()));

        List<TongJiVO> tongJiVOS=new ArrayList<>();
        tongJiVOS.add(benchmarkVO);
        tongJiVOS.add(gpsVO);
        return ResultVOUtil.success(tongJiVOS);
    }

    @PostMapping("/countcity")
    public ResultVO countCity(){
        List<Object> listBenchmark=benchmarkService.countCity();
        List<CityVO> cityVOS=new ArrayList<>();
        CityVO cityVO;
        for (Object o:listBenchmark){
            cityVO=new CityVO();
            cityVO.setName(((Object[]) o)[0].toString());
            cityVO.setWcs(Integer.parseInt(( ((Object[]) o)[1]).toString()));
            cityVO.setZs(Integer.parseInt(( ((Object[]) o)[2]).toString()));
            cityVOS.add(cityVO);
        }

        return ResultVOUtil.success(cityVOS);
    }

    @PostMapping("/countcountry")
    public ResultVO countCity(String city){
        List<Object> listBenchmark=benchmarkService.countCountry(city);
        List<CityVO> cityVOS=new ArrayList<>();
        CityVO cityVO;
        for (Object o:listBenchmark){
            cityVO=new CityVO();
            cityVO.setName(((Object[]) o)[0].toString());
            cityVO.setWcs(Integer.parseInt(( ((Object[]) o)[1]).toString()));
            cityVO.setZs(Integer.parseInt(( ((Object[]) o)[2]).toString()));
            cityVOS.add(cityVO);
        }
        return ResultVOUtil.success(cityVOS);
    }
    @PostMapping("/countallcountry")
    public ResultVO countAllCity(Integer pageNum, Integer pageSize){
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<Object> listBenchmark=benchmarkService.countAllCountry(pageable);
        List<CityVO> cityVOS=new ArrayList<>();
        CityVO cityVO;
        for (Object o:listBenchmark.getContent()){
            cityVO=new CityVO();
            cityVO.setName(((Object[]) o)[0].toString());
            cityVO.setWcs(Integer.parseInt(( ((Object[]) o)[1]).toString()));
            cityVO.setZs(Integer.parseInt(( ((Object[]) o)[2]).toString()));
            cityVOS.add(cityVO);
        }
        return ResultVOUtil.successforCount(cityVOS,listBenchmark.getTotalElements());
    }

    @PostMapping("/countbyname")
    public ResultVO countByNmame(String name){
        List<Object> listBenchmark=benchmarkService.countByName(name);
        List<CityVO> cityVOS=new ArrayList<>();
        CityVO cityVO;
        for (Object o:listBenchmark){
            cityVO=new CityVO();
            cityVO.setName(((Object[]) o)[0].toString());
            cityVO.setWcs(Integer.parseInt(( ((Object[]) o)[1]).toString()));
            cityVO.setZs(Integer.parseInt(( ((Object[]) o)[2]).toString()));
            cityVOS.add(cityVO);
        }
        return ResultVOUtil.success(cityVOS);
    }
}
